#coding:utf-8
"""
*   Coder    : Dzlua
*   Email    : 505544956@qq.com
*   Time     : 2017/09/29
"""
#--------------------#
from flask_script import Command
from app.utilities import helper

#--------------------#
def create_admin(db):
    """Creates the admin user."""
    from app.models.auth import datastore, User
    #
    if not User.query.first():
        datastore.create_user(email='dzlua@dzlua.com', password='dzlua')
        db.session.commit()
    #
#--------------------#
class Deploy(Command):
    '''deploy server.'''
    def run(self):
        print('install database...')
        helper.db.create_all()
        print('install database done!')
        print('create admin user...')
        create_admin(helper.db)
        print('create admin user done!')
#----------#
class Dropall(Command):
    '''drop all tables.'''
    def run(self):
        helper.db.drop_all()
        print('all tables dropped!')
#--------------------#
class Cmds:
    def __init__(self, mgr):
        self.mgr = mgr
        mgr.manager.add_command('deploy', Deploy)
        mgr.manager.add_command('dropall', Dropall)

#--------------------#
